package com.arif.app.sulamunnayirain;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Html;
import android.text.Layout;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.ScrollView;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.Toast;

import com.arif.alg.sulamunnayirain.Record;
import com.arif.alg.sulamunnayirain.SulamunNayyirain;

public class OutputActivity extends Activity{
	private TextView tvTitle;
	private Button buttonTampil;
	
	Record rc;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_output);
		ScrollView sv = (ScrollView)findViewById(R.id.scrollViewOutput);
		TableLayout tableLayout = new TableLayout(this);
		tableLayout.setStretchAllColumns(true);		
		tableLayout.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.MATCH_PARENT));
		tvTitle= new TextView(this);
		// get extra from MainActity
		Intent i=getIntent();
		int ibulan = i.getIntExtra(ConstantValue.KEY_BULAN, -1);
		int itahun =i.getIntExtra(ConstantValue.KEY_TAHUN, -1);
		int ikota = i.getIntExtra(ConstantValue.KEY_KOTA, -1);
		String sbulan = i.getStringExtra(ConstantValue.KEY_BULAN_NAME);
		String skota = i.getStringExtra(ConstantValue.KEY_KOTA_NAME);
		if(ikota == 22){
			ikota = 26;
		}
		try {
			ConstantValue.sulamunNayyirain = new SulamunNayyirain();
			rc=ConstantValue.sulamunNayyirain.process(itahun, (ibulan==0?11:ibulan-1), ikota);			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			Toast t= Toast.makeText(OutputActivity.this, e.getMessage(), Toast.LENGTH_LONG);
			t.show();
			return;
		}
		// set textView showing the result
		StringBuilder title=new StringBuilder();
		title.append("Hasil Perhitungan").append("<br><table>");
		title.append("<small><br>Awal Bulan : <br>\t<b>").append(sbulan).append("</b>");
		title.append("<br>Tahun : <br>\t<b>").append(itahun).append("</b>");
		title.append("<br>Kota : <br>\t<b>").append(skota).append("</b>");
		title.append("<br>Hari : <br>\t<b>").append(rc.getHari()).append("</b>");
		title.append("<br>Hari Ijtimah : <br>\t<b>").append(rc.getHariIjtimah()).append("</b>");
		title.append("<br>Waktu : <br>\t<b>").append(rc.getJam()).append("</b>");
		title.append("<br>Tinggi Hilal : <br>\t<b>").append(rc.getTinggiHilal()).append("</b></small>");
		// tetep iman
		// padang hati -> mudah bersyukur
		// selamat dunia - akhirat		
		tvTitle.setText(Html.fromHtml(title.toString()));
		tvTitle.setGravity(android.view.Gravity.RIGHT);
		
		
		TableRow row1 = new TableRow(this);
		TableRow row2 = new TableRow(this);
		TableRow row3 = new TableRow(this);
		
		
		//row1.addView(tvTitle);
		TextView tvJudul = new TextView(this);
		tvJudul.setTextSize(25.0f);
		tvJudul.setText(Html.fromHtml("Perhitungan Awal Bulan"));
		
		TextView tvSubjudul = new TextView(this);
		tvSubjudul.setText(sbulan+" "+itahun+ " H. Kota "+skota);		
		
		TextView labelIjtima = new TextView(this);
		labelIjtima.setText(Html.fromHtml("<br>HASIL IJTIMA"));
		labelIjtima.setTextSize(20.0f);
		
		TextView tvHari = new TextView(this);
		tvHari.setText(rc.getHari());
		
		row1.addView(tvJudul);
		row2.addView(tvSubjudul);
		
		TableLayout t2 = new TableLayout(this);
		t2.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT));
		TableLayout t3 = new TableLayout(this);
		t3.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT));
		
			TableRow[] rowContent = new TableRow[3];
			TextView[] labelContent = new TextView[3];
			TextView[] tvContent = new TextView[3];
			String[] arrLabel = {"Hari","Waktu","Tinggi Hilal"};
			String[] arrContent = {rc.getHariIjtimah(),rc.getJam(),rc.getTinggiHilal()};
			for(int x=0;x<3;x++){
				rowContent[x] = new TableRow(this);
				labelContent[x] = new TextView(this);			
				labelContent[x].setText(arrLabel[x]);
				tvContent[x] = new TextView(this);
				tvContent[x].setText(Html.fromHtml(": "+arrContent[x]+"<br>"));
				rowContent[x].addView(labelContent[x]);
				rowContent[x].addView(tvContent[x]);
				t2.addView(rowContent[x]);
			}
				
		TextView labelKesimpulan = new TextView(this);			
		labelKesimpulan.setText("Awal Bulan Jatuh Pada:");
		labelKesimpulan.setGravity(Gravity.CENTER);
		
		TextView tvKesimpulan = new TextView(this);
		tvKesimpulan.setText(Html.fromHtml("<b>Hari "+rc.getHari()+" 1 "+sbulan+" "+itahun+" H</b>"));
		tvKesimpulan.setGravity(Gravity.CENTER);
		
			
		TextView textCopy=new TextView(this);
        textCopy.setText(Html.fromHtml("<br>&copy;2014 Sullamun Nayyirain<br>v1.0<br>"));
        textCopy.setGravity(Gravity.CENTER);
		
        Button buttonProcess = new Button(this);
        buttonProcess.setText("Lihat Process");

        buttonProcess.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View arg0) {			
				
				Intent i=new Intent(OutputActivity.this, ProcessActivity.class);
				startActivity(i);				
			}
		});
        
		tableLayout.addView(row1);
		tableLayout.addView(row2);
		tableLayout.addView(labelIjtima);
		tableLayout.addView(t2);
		tableLayout.addView(labelKesimpulan);
		tableLayout.addView(tvKesimpulan);				
        tableLayout.addView(textCopy);
        tableLayout.addView(buttonProcess);
        
        //tableLayout.setPadding(10, 10, 10, 10);
        sv.addView(tableLayout);
				
		
	}
}

